package com.nx.qwr;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author: Qingwr
 * @Date: 2022/8/14 20:51
 * @Description 流量实体类
 */
public class FlowBean implements WritableComparable<FlowBean> {

    //电话号码
    private String phone;
    //上行数据包
    private int upFlow;
    //下行数据包
    private int downFlow;
    //上行流量
    private int upCountFlow;
    //下行流量
    private int downCountFlow;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(int upFlow) {
        this.upFlow = upFlow;
    }

    public int getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(int downFlow) {
        this.downFlow = downFlow;
    }

    public int getUpCountFlow() {
        return upCountFlow;
    }

    public void setUpCountFlow(int upCountFlow) {
        this.upCountFlow = upCountFlow;
    }

    public int getDownCountFlow() {
        return downCountFlow;
    }

    public void setDownCountFlow(int downCountFlow) {
        this.downCountFlow = downCountFlow;
    }

    public FlowBean() {
    }

    public FlowBean(String phone, int upFlow, int downFlow, int upCountFlow, int downCountFlow) {
        this.phone = phone;
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.upCountFlow = upCountFlow;
        this.downCountFlow = downCountFlow;
    }


    @Override
    public String toString() {
        return phone + ":\t" + upFlow + "\t" + downFlow + "\t" + upCountFlow + "\t" + downCountFlow;
    }

    @Override
    public int compareTo(FlowBean o) {
        //当值相等时如果返回0,输出只会保留一项. 即多行中upFlow相等的话只会保留其中一条//TODO
        if(this.upFlow == o.upFlow){
            return -1;
        }
        return -(this.upFlow - o.upFlow);
    }

    //序列化, 注意写入类型和顺序,要和readFields保持一致
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(phone);
        dataOutput.writeInt(upFlow);
        dataOutput.writeInt(downFlow);
        dataOutput.writeInt(upCountFlow);
        dataOutput.writeInt(downCountFlow);
    }
    //反序列化
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.phone = dataInput.readUTF();
        this.upFlow = dataInput.readInt();
        this.downFlow = dataInput.readInt();
        this.upCountFlow = dataInput.readInt();
        this.downCountFlow = dataInput.readInt();
    }
}
